home *** CD-ROM | disk | FTP | other *** search
/ Animation / Animation Vol.1 (Profi ROM)(1994).iso / pool / updates / symantec / rtlinc.exe / STREAM.H < prev    next >
C/C++ Source or Header  |  1993-06-15  |  8KB  |  329 lines

  1. //_ stream.hpp   Thu Mar  1 1990   Modified by: Walter Bright */
  2. //               Fri Aug 18 1989   Modified by: Steve Teale
  3.  
  4. #ifndef __STREAM_H
  5. #define __STREAM_H
  6.  
  7. extern "C++" {
  8.  
  9. #ifndef __STDIO_H
  10. #include    <stdio.h>
  11. #endif
  12.  
  13. #ifndef    BUFSIZE
  14. #define    BUFSIZE    1024
  15. #endif
  16.  
  17. ///////////////////////////// STREAMBUF //////////////////////
  18.  
  19. // Class for dealing with a stream buffer
  20. struct streambuf
  21. {
  22. protected:
  23.     char *base;        // buffer, NULL if no buffer
  24.     char *pptr;        // write pointer (1 past end of data in buffer)
  25.     char *gptr;        // read pointer (next char to read), gptr chases pptr
  26.     char *eptr;        // 1 past end of buffer
  27.     char alloc;        // 1 if base was allocated using new
  28.     char dummy;        // for alignment
  29.     FILE *fp;        // in case streambuf is just a layer over stdio
  30.  
  31.     int doallocate();
  32.     int allocate() { return base ? 0 : doallocate(); }
  33.     streambuf *setbuf(char *buf, int buflen,
  34.             int written = 0, int wasalloc = 0);
  35.  
  36.     friend class ostream;
  37.     friend class istream;
  38.  
  39. public:
  40.  
  41. // Functions for buffer full and empty respectively
  42.     virtual int overflow(int c = EOF);
  43.     virtual int underflow();
  44.  
  45. // Constructors
  46.     streambuf();
  47.     streambuf(char* buf, int buflen);
  48.  
  49. // Destructor
  50.     virtual ~streambuf();
  51.  
  52. // Character by character functions
  53.     int snextc()
  54.     {    return (gptr + 1 < pptr)
  55.         ? (unsigned char) *++gptr
  56.         : underflow();
  57.     }
  58.  
  59.     int sgetc()
  60.     {    return (gptr < pptr)
  61.         ? (unsigned char) *gptr
  62.         : underflow();
  63.     }
  64.  
  65.     void stossc()
  66.     {   (++gptr > pptr) && underflow(); }
  67.  
  68.     int sputc(int c = EOF)
  69.     {
  70.         return fp
  71.             ? putc(c,fp)
  72.             : (pptr < eptr)
  73.                 ? (unsigned char) (*pptr++ = c)
  74.                 : overflow(c);
  75.     }
  76.  
  77.     void sputbackc(char c)
  78.         { (gptr > base) && (*--gptr = c) != 0; }
  79.  
  80. // Access to buffer
  81.     char *bufptr() { return base; }
  82. };
  83.  
  84. /////////////////////// FILEBUF ///////////////////////
  85.  
  86. extern int __CLIB close(int);        // should match io.h
  87.  
  88. // a stream buffer for files
  89.  
  90. enum open_mode  { input = 0, output = 1, append = 2 };
  91.  
  92. struct filebuf : public streambuf
  93. {
  94.     int    fd;
  95.     char opened;
  96.     char dummy;            // for alignment
  97.  
  98. public:
  99.  
  100.     int overflow(int c = EOF);
  101.     int underflow();
  102.     
  103.     filebuf* open(char *name,open_mode om);
  104.     int    close();
  105.  
  106.     filebuf();
  107.     filebuf(int _fd);    // file descriptor
  108.     filebuf(FILE* p);
  109.     filebuf(int _fd, char *buf, int buflen);
  110.  
  111.     ~filebuf();
  112. };
  113.  
  114. ///////////////////////// CIRCBUF //////////////////////
  115. // Circular stream buffer
  116.  
  117. struct circbuf : public streambuf
  118. {
  119.     int overflow(int c = EOF);
  120.     int underflow();
  121.  
  122.      circbuf();
  123.     ~circbuf();
  124. };
  125.  
  126. ///////////////////// Input and Output ///////////////////
  127.  
  128. struct whitespace { int dummy; };
  129.  
  130. // State for each istream or ostream
  131. enum state_value
  132. {    _good = 0,    // previous input operation succeeded. state must
  133.             // be _good for subsequent input operations to succeed
  134.     _eof  = 1,    // reached end of file
  135.     _fail = 2,    // error, no characters lost
  136.     _bad  = 4    // the stream is all messed up
  137. };
  138.  
  139. // Output formatting routines
  140.  
  141. extern char *dec(long, int = 0);
  142. extern char *oct(long, int = 0);
  143. extern char *hex(long, int = 0);
  144. extern char *str(const char *, int = 0);
  145. extern char *chr(int, int = 0);
  146. extern char *form(const char * ...);
  147.  
  148.  
  149. ///////////////////////// OSTREAM //////////////////////
  150.  
  151. class ostream
  152. {
  153.     streambuf *bp;
  154.     state_value state;
  155.     int alloc;
  156.  
  157.     friend class istream;
  158.  
  159. public:
  160.  
  161. // Overloads of <<
  162.     ostream& operator<<(streambuf&);
  163.     ostream& operator<<(const whitespace&);
  164.     ostream& operator<<(const char*);
  165.     ostream& operator<<(const signed char *psc)
  166.     { return *this << (const char *) psc; }
  167.     ostream& operator<<(const unsigned char *puc)
  168.     { return *this << (const char *) puc; }
  169.     ostream& operator<<(long);
  170.     ostream& operator<<(unsigned long ul);
  171.     ostream& operator<<(int a) { return *this << (long) a; }
  172.     ostream& operator<<(unsigned a) { return *this << (long) a; }
  173.     ostream& operator<<(char c);
  174.     ostream& operator<<(signed char c) { return *this << (char) c; }
  175.     ostream& operator<<(unsigned char c) { return *this << (char) c; }
  176.     ostream& operator<<(short s) { return *this << (int) s; }
  177.     ostream& operator<<(unsigned short us) { return *this << (unsigned) us; }
  178.     ostream& operator<<(double);
  179.     ostream& operator<<(const void *);
  180.  
  181. // Other output functions
  182.     ostream& flush()
  183.         { bp->overflow(); return *this; }
  184.     ostream& put(char);
  185.  
  186. // Stream state access functions
  187.     int    good()            { return state == _good; }
  188.     int    eof()            { return state & _eof; }
  189.     int    fail()              { return state & (_fail | _bad); }
  190.     int    bad()            { return state & _bad; }
  191.  
  192.     operator void *()        { return fail() ? NULL : this; }
  193.     int    operator !()        { return fail(); }
  194.     int    rdstate()        { return state; }
  195.  
  196. // State set function
  197.     void clear(state_value v = _good) { state = v; }
  198.  
  199. // Access to associated buffer
  200.     char *bufptr() { return bp->bufptr(); }
  201.  
  202. // Constructors
  203.     ostream(streambuf *sb);
  204.     ostream(int fd);
  205.     ostream(int buflen, char *buf);
  206.  
  207. // Destructor
  208.     ~ostream() { flush(); if (alloc) delete bp; }
  209. };
  210.  
  211. ///////////////////////// ISTREAM ///////////////////////////
  212.  
  213. class istream
  214. {
  215.     streambuf    *bp;
  216.     ostream    *tied_to;
  217.     state_value    state;
  218.     char    skipws;
  219.     char    alloc;
  220.  
  221.     void eatwhite();
  222.  
  223. public:
  224.  
  225. // Overloads of operator>>
  226.     istream& operator>>(streambuf&);
  227.     istream& operator>>(whitespace&);
  228.     istream& operator>>(char&);
  229.     istream& operator>>(char*);
  230.     istream& operator>>(signed char &sc) { return *this >> (char ) sc; }
  231.     istream& operator>>(signed char *p)  { return *this >> (char *) p; }
  232.     istream& operator>>(unsigned char &uc) { return *this >> (char ) uc; }
  233.     istream& operator>>(unsigned char *p)  { return *this >> (char *) p; }
  234.     istream& operator>>(int&);
  235.     istream& operator>>(unsigned &u) { return *this >> (int) u; }
  236. #if __INTSIZE == 4
  237.     istream& operator>>(short &s) { int i ; *this >> i; s = i; return *this; }
  238.     istream& operator>>(unsigned short &us) { int i; *this >> i; us = i; return *this; }
  239. #else
  240.     istream& operator>>(short &s) { return *this >> (int) s; }
  241.     istream& operator>>(unsigned short &us) { return *this >> (int) us; }
  242. #endif
  243.     istream& operator>>(long&);
  244.     istream& operator>>(unsigned long &ul) { return *this >> (long) ul; }
  245.     istream& operator>>(float&);
  246.     istream& operator>>(double&);
  247.  
  248. // Other input functions
  249.     istream& get(char *, int, char = '\n');
  250.     istream& get(streambuf&, char = '\n');
  251.     istream& get(char& c);
  252.     istream& putback(char);
  253.  
  254. // Istream control functions
  255.     ostream* tie(ostream *os);
  256.     int    skip(int s);
  257.  
  258. // Stream state access functions
  259.     int    good()          { return state == _good; }
  260.     int    eof()        { return state & _eof; }
  261.     int    fail()          { return state & (_fail | _bad); }
  262.     int    bad()        { return state & _bad; }
  263.  
  264.     int    operator!()    { return fail(); }
  265.     operator void*()    { return fail() ? 0 : this; }
  266.     int    rdstate()    { return state; }
  267.  
  268. // Stream state set function
  269.     void clear(state_value v = _good) { state = v; }
  270.  
  271.     char *bufptr()    { return bp->bufptr(); }    
  272.  
  273. // Constructors
  274.     istream(int len, char *string, int s = 1);
  275.     istream(streambuf *sb, int s = 1, ostream *os = NULL);
  276.     istream(int fd, int s = 1, ostream *os = NULL);
  277.  
  278. // Destructor
  279.     ~istream();
  280. };
  281.  
  282.  
  283. /////////////////////
  284. // Predefined I/O streams.
  285. // These are tied to stdin, stdout, stderr, stdprn, stdaux
  286.  
  287. extern istream cin;
  288. extern ostream cout;
  289. extern ostream cerr;
  290. #if !(M_UNIX || M_XENIX)
  291. extern ostream cprn;
  292. extern ostream caux;
  293. #endif
  294.  
  295. //////////////////////////////
  296. // Embed correct library into .OBJ file
  297.  
  298. #if __SC__ >= 0x325
  299. #if __INTSIZE == 4
  300.  
  301. #if __NT__
  302. #pragma SC includelib "oldstrn"
  303. #elif __OS2__
  304. #pragma SC includelib "oldstrf"
  305. #else
  306. #pragma SC includelib "oldstrx"
  307. #endif
  308.  
  309. #else
  310.  
  311. #if __SMALL__
  312. #pragma SC includelib